package com.springStudy.boot.redis.seconedDemo.jedis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhengliangtian on 2019/1/8.
 * @version 1.0
 */

@Configuration
public class RedisConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${spring.session.redis.host:47.99.209.115}")
    private String host;
    @Value("${spring.session.redis.port:6379}")
    private String port;
    @Value("${spring.session.redis.database:0}")
    private String database;
    @Value("${spring.session.redis.password:}")
    private String password;
    @Value("${spring.session.redis.pool.max-active:8}")
    private String maxActive;
    @Value("${spring.session.redis.pool.max-idle:8}")
    private String maxIdle;
    @Value("${spring.session.redis.pool.max-wait:-1}")
    private String maxWait;
    @Value("${spring.session.redis.pool.min-idle:0}")
    private String minIdle;
    @Value("${spring.session.redis.timeout:}")
    private String timeout;

    private static Map<String, JedisPool> maps = new ConcurrentHashMap<String, JedisPool>();

    @Bean("jedisPoolConfig")
    public JedisPoolConfig getPoolConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(maxActive));
        config.setMaxIdle(Integer.parseInt(maxIdle));
        config.setMaxWaitMillis(Long.parseLong(maxWait));
        config.setMinIdle(Integer.parseInt(minIdle));
        return config;
    }

    @Bean("jedisConnectionFactory")
    public JedisConnectionFactory getConnectionFactory(@Qualifier("jedisPoolConfig") JedisPoolConfig config) {
        JedisConnectionFactory con = new JedisConnectionFactory();
        con.setDatabase(Integer.parseInt(database));
        con.setHostName(host);
        con.setPassword(password);
        con.setTimeout(Integer.parseInt("10000"));
        con.setPort(Integer.parseInt(port));
        con.setPoolConfig(config);
        return con;
    }

    @Bean("redisTemplate")
    public RedisTemplate<String, Object> getRedisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 开启事务
        redisTemplate.setEnableTransactionSupport(true);
        return redisTemplate;
    }

    @Bean(value = "stringRedisTemplate")
    public StringRedisTemplate getStringRedisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(jedisConnectionFactory);
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
        stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setHashValueSerializer(new StringRedisSerializer());
        // 开启事务
        stringRedisTemplate.setEnableTransactionSupport(true);
        return stringRedisTemplate;
    }

}
